<?php

namespace App\Morefun\Prize;

use App\Contracts\Services\Storage\StorageService;
use App\Entities\ActivityPrize;
use App\Entities\PrizeType;
use App\Entities\VoucherCode;
use Illuminate\Container\Container;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;

class RedisSynchronizer
{
    /**
     * @var \Redis
     */
    protected $redis;

    /**
     * @var StorageService
     */
    protected $storage;

    public function write($activityId)
    {
        $prizes = $this->formatPrizesOfActivity($activityId);
        $key = sprintf('activity:%s:prizes', $activityId);

        return $this->redis()->set($key, json_encode($prizes));
    }

    protected function formatPrizesOfActivity($activityId)
    {
        usleep(2000);
        $bindings = ActivityPrize::with('prize', 'prize.type', 'prize.sponsors')
            ->where('activity_id', $activityId)
            ->where('is_recycle', 0)
            ->get();

        $formatted = [];
        $storage = Container::getInstance()->make(StorageService::class);

        foreach ($bindings as $of => $binding) {
            $fileds = [];

            if (!empty($binding->prize->extends)) {
                $fileds = json_decode($binding->prize->extends, true);
            }

            $formatted[] = [
                'name' => $binding->prize->name,
                'alias_name' => $binding->alias_name,
                'id' => $binding->prize->id,
                'type_name' => $binding->prize->type->name,
                'type_code' => $binding->prize->type->code,
                'exchange_method' => $binding->prize->exchange_method,
                'exchange_address' => $binding->prize->exchange_address,
                'exchange_url' => $binding->prize->exchange_url,
                'voucher_code' => strval($binding->prize->voucher_code),
                'voucher_type' => $binding->prize->voucher_type,
                'stock_limit' => $binding->stock_limit,
                'image' => $storage->toHttpUrl($binding->prize->background_image),
                'surplus' => $binding->surplus,
                'max_amount' => $binding->max_amount,
                'winning_limit' => $binding->winning_limit,
                'winning_probability' => $binding->winning_probability,
                'fileds' => $fileds,
                'expired_start' => strtotime($binding->prize->expired_start),
                'expired_end' => strtotime($binding->prize->expired_end),
                'sponsors' => $this->formatSponsors($binding->prize->sponsors),
            ];
        }

        return $formatted;
    }

    protected function redis()
    {
        if (!$this->redis) {
            $this->redis = Container::getInstance()->make('redis');
        }

        return $this->redis;
    }

    protected function storage()
    {
        if (!$this->storage) {
            $this->storage = Container::getInstance()->make(StorageService::class);
        }

        return $this->storage;
    }

    protected function formatSponsors(Collection $sponsors = null)
    {
        if (!$sponsors) {
            return [];
        }

        $formatted = [];

        foreach ($sponsors as $sponsor) {
            $formatted[] = [
                'name' => $sponsor->name,
                'logo' => $this->storage()->toHttpUrl($sponsor->logo),
                'percent' => $sponsor->percent,
            ];
        }

        return $formatted;
    }
}
